$(function(){
	/* 添加图片 */
	$(".photos-upLoad").on("click", ".add", function () {
		$(this).siblings("input:file").trigger("click");
	}).on("click", ".del", function () {

		$(this).parents(".photoItem").remove();

		//没有上传图片则显示
		if($("div").siblings(".maxImgLen").length == 0){
			$(".photos-upLoad .clear").prev().show();
		}
	});
	$(".photos-upLoad").on("change","input:file",function () {


		if($(this).siblings(".add").length < 1)return false;

		var parent  = $(this).parent();
		if (!/\.(jpg|png|jpeg)$/.test(this.value)) {
			alert("请选择jpg png jpeg格式的图片!");
			this.value = "";
			return false;
		}

		var img = new Image();
		if (this.files && this.files[0]) {
			img.src = window.URL.createObjectURL(this.files[0]);
		} else {

			$(this).css({"display":"block","width":"0","height":"0"}).show();			
   			this.select();
			var reallocalpath = document.selection.createRange().text;
			img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+reallocalpath+"',sizingMethod=scale)";
			img.src= '';
                        
		}


		//上传张数
		/***************		
		*在上传页面设置 
		*如果没有设置默认1张
		****************/
		var maxImgLen=$("#maxImgLen").val();		
		if(typeof(maxImgLen)=="undefined"){
			maxImgLen=1;
		}
		if ($(".photoItem").length <= maxImgLen) {
			var div = $("<div class='photoItem maxImgLen'><a href='javascript:void(0)' class='del'></a></div>");
			div.append($("<div class='img'>").append(img)).append(this);
			$(".photoItem:has(.add)").before(div);

			var clone =$(this).clone();
			clone.val("");
			parent.append(clone);

			//超出上传张数则隐藏
			if($("div").siblings(".maxImgLen").length == maxImgLen){
				$(".photos-upLoad .clear").prev().hide();
			}	

		}else{			
			alert("最多只能上传"+maxImgLen+"张图片");				
		}
				
	});
	
	
});